
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;

import java.io.IOException;


public class Main {
    /**
     * 破解mybatisplugins
     * @param args
     * @throws NotFoundException
     *
     * 1、配置pom.xml
     *          <dependency>
     *              <groupId>org.javassist</groupId>
     *              <artifactId>javassist</artifactId>
     *              <version>3.21.0-GA</version>
     *          </dependency>
     * 2、找到 mybatis插件的路径，替换pool中mybatis插件的路径
     * 3、运行Main类中main方法
     * 4、把mybatis插件下的 mybatis_plus.jar包复制到当前项目生成的 hello文件夹下
     * 5、cmd 命令行进入到 hello文件夹下，运行 jar uvf mybatis_plus.jar com
     * 6、把 hello中替换后的 mybatis_plus.jar包，重新放入回 mybatis插件下
     * 7、破解完毕！可以删除pom.xml 中Javaassist配置
     *
     */
    public static void main(String[] args) throws NotFoundException {
        ClassPool pool = ClassPool.getDefault();
        //pool.appendClassPath("E:\\xxx\\xxx\\target\\xxx_war\\WEB-INF\\lib\\xxx_jar-0.0.1-SNAPSHOT.jar");
        pool.appendClassPath("C:\\Users\\Administrator\\.IntelliJIdea2016.3\\config\\plugins\\mybatis_plus\\lib\\mybatis_plus.jar");
        CtClass driverClass = pool.get("com.seventh7.mybatis.ref.license.ActivationDriver");
        CtClass javaUtil = pool.get("com.seventh7.mybatis.util.JavaUtils");
        CtMethod activate = driverClass.getMethod("activate", "(Ljava/lang/String;)Lcom/seventh7/mybatis/ref/license/ActivationResult;");
        CtMethod refValid = javaUtil.getDeclaredMethod("refValid");
        try {
            refValid.setBody("{return true;}");
        } catch (CannotCompileException e) {
            e.printStackTrace();
        }
        System.out.println(activate);
        try {
            activate.setBody("{com.seventh7.mybatis.ref.license.LicenseData licenseData = new com.seventh7.mybatis.ref.license.LicenseData(\"1\", \"2\");com.seventh7.mybatis.ref.license.ActivationResult res =com.seventh7.mybatis.ref.license.ActivationResult.success(licenseData); return res;}");
        } catch (CannotCompileException e) {
            e.printStackTrace();
        }
        try {
            driverClass.writeFile("hello");
            javaUtil.writeFile("hello");
        } catch (CannotCompileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


